package com.mive.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("redis.do")
public class RedisController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;



    @RequestMapping(value = "/setVal")
    public String setVal(){
        if (!stringRedisTemplate.hasKey( "test" )) {
            stringRedisTemplate.opsForValue().set("test","test");
            return "使用redis保存数据成功";
        }else {
            return "key已经存在";
        }
    }

    @RequestMapping(value = "getVal")
    public String getVal(){
        if (!stringRedisTemplate.hasKey( "test" )) {
            return "该key不存在";
        }else{
            String val = stringRedisTemplate.opsForValue().get("test");
            return val;
        }
    }
}
